<template>
  <div>
    <el-card class="box-card">
      <HeaderInput :getHeaderParam="getHeaderParam" @click="addOrupdateAttr" @queryAttrList="queryAttrList"
                   :addOrUpdateTagShow="addOrUpdateTagShow"></HeaderInput>
    </el-card>
    <el-card class="box-card">
      <div v-show="!addOrUpdateTagShow">
        <el-button type="primary" round icon="el-icon-plus" @click="addOrupdateAttr" :disabled="!categoryId3">添加属性
        </el-button>
        <el-table
          :data="attrList"
          border
          style="width: 100%">
          <el-table-column
            type="index"
            label="序号"
            align="center"
            width="180">
          </el-table-column>
          <el-table-column
            prop="attrName"
            label="属性名称"
            width="250">
          </el-table-column>
          <el-table-column
            label="属性值列表">
            <template slot-scope="{row}">
              <el-tag style="margin: 0px 10px;" type="success" v-for="r in row.attrValueList" :key="r.id">{{
                  r.valueName
                }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            width="250">
            <template slot-scope="{row,$index}">
              <el-button
                size="mini"
                @click="addOrupdateAttr(row)"
              >修改
              </el-button>
              <el-popconfirm title="您确定要删除吗？" @onConfirm="deleteAttr(row)">
              <el-button
                size="mini"
                type="danger"
                slot="reference"
              >删除
              </el-button>

              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div v-show="addOrUpdateTagShow">
        <el-form :inline="true" :model="addForm" class="demo-form-inline">
          <el-form-item label="属性名称">
            <el-input v-model="addForm.attrName" placeholder="请输入属性名称"></el-input>
          </el-form-item>
        </el-form>
        <el-button type="primary" round icon="el-icon-plus" @click="addAttr" :disabled="!addForm.attrName">添加属性值
        </el-button>
        <el-table
          :data="addForm.attrValueList"
          border
          style="width: 100%">
          <el-table-column
            type="index"
            label="序号"
            align="center"
            width="180">
          </el-table-column>
          <el-table-column
            label="属性值名称">
            <template slot-scope="{row,$index}">
              <el-input :ref="refsName+$index" v-if="row.inputShow" v-model="row.valueName" placeholder="请输入属性名称"
                        @blur="lostFocus(row)" @keyup.enter="lostFocus(row)"></el-input>
              <span style="display: block" v-else @click="clickSpan(row,$index)">{{ row.valueName }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            width="250">
            <template slot-scope="{row,$index}">
              <el-button
                size="mini"
                type="danger"
                @click="handleDelete($index)"
              >删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-button type="primary" round icon="el-icon-plus" @click="saveAttr"
                   :disabled="addForm.attrValueList.length==0 || addForm.attrValueList.some(e=>e.valueName.trim().length==0) || !addForm.attrName">
          保存
        </el-button>
        <el-button type="danger" round icon="el-icon-delete-solid" @click="addOrUpdateTagShow=false">取消保存</el-button>
      </div>
    </el-card>

  </div>
</template>

<script>
import HeaderInput from "@/views/product/HeaderInput";
import {getAttrList,saveAttr,deleteAttr} from '@/api/swagger'
import cloneDeep from 'lodash/cloneDeep'

export default {
  name: "AttrVue",
  data() {
    return {
      attrList: [],
      addOrUpdateTagShow: false,
      categoryId1: '',
      categoryId2: '',
      categoryId3: '',
      refsName: 'refs1_',
      addForm: {
        attrName: '',
        attrValueList: [],
        attrId: '',
      }
    }
  },
  components: {HeaderInput},
  // 方法集合
  methods: {
    async getHeaderParam(headerForm) {
      this.categoryId1 = headerForm.categoryId1
      this.categoryId2 = headerForm.categoryId2
      this.categoryId3 = headerForm.categoryId3
    },
    addOrupdateAttr(row) {
      this.addForm.attrName = cloneDeep(row.attrName ? row.attrName : '')
      this.addForm.attrValueList = row.attrValueList ? cloneDeep(row.attrValueList) : []
      this.addForm.attrId = cloneDeep(row.id ? row.id : '')
      this.addOrUpdateTagShow = true
      this.addForm.attrValueList.forEach(e => {
        this.$set(e, "inputShow", false,)
      })

    },
    async queryAttrList() {
      await getAttrList(this.categoryId1, this.categoryId2, this.categoryId3).then(res => {
        if (res.code = 200) {
          this.attrList = res.data
        }
      })
    }, addAttr() {
      this.addForm.attrValueList.push({
        attrId: '',
        valueName: '',
        inputShow: true,
        id: ''
      })
      this.$nextTick(() => {
        this.$refs[this.refsName + (this.addForm.attrValueList.length - 1)].focus()

      })
    },
    clickSpan(row, $index) {
      row.inputShow = true
      this.$nextTick(() => {
        this.$refs[this.refsName + $index].focus()
      })
    }, lostFocus(row) {
      if (row.valueName.trim().length > 0) {
        row.inputShow = false
      } else {
        this.$message({type: "warning", message: '请输入合法的内容'})
      }
    }, handleDelete($index) {
      this.addForm.attrValueList.splice($index, 1)
    },
    async saveAttr() {
      const param = {
        attrName: this.addForm.attrName,
        attrValueList: this.addForm.attrValueList,
        categoryId: this.categoryId3,
        categoryLevel: '3',
        id: this.addForm.attrId
      }
    await saveAttr(param).then(res=>{
      if (res.code==200){
        this.$message({type:"success",message:'保存属性成功！'})
        this.addOrUpdateTagShow=false;
        this.queryAttrList();
      }
    })
    },
    async deleteAttr(row){
      await deleteAttr(row.id).then(res=>{
        if (res.code=200){
          this.$message({type:"success",message:'删除属性成功！'})
          this.queryAttrList();
        }
      })
    }
  },

}
</script>

<style scoped>

</style>
